package com.asiainfo.dacp.datastash.open.controller;

import com.asiainfo.dacp.datastash.domain.log.models.DataStashUnitEventLog;
import com.asiainfo.dacp.datastash.domain.log.models.DataStashUnitTaskEventLog;
import com.asiainfo.dacp.datastash.domain.log.repos.DataStashUnitEventLogRepository;
import com.asiainfo.dacp.datastash.domain.log.repos.DataStashUnitTaskEventLogRepository;
import com.asiainfo.dacp.datastash.domain.state.models.DataStashUnitStateLog;
import com.asiainfo.dacp.datastash.domain.state.models.DataStashUnitTaskStateLog;
import com.asiainfo.dacp.datastash.domain.state.repos.DataStashUnitStateLogRepository;
import com.asiainfo.dacp.datastash.domain.state.repos.DataStashUnitTaskStateLogRepository;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by winter on 2017/12/25.
 */
@RestController
@RequestMapping("/open/datastash")
@Api(tags={"datastash-unit-state"},value="单元状态日志")
public class DataStashUnitStateController {

    @Autowired
    private DataStashUnitStateLogRepository unitStateLogRepo;

    @Autowired
    private DataStashUnitTaskStateLogRepository unitTaskStateLogRepo;

    @Autowired
    private DataStashUnitEventLogRepository unitEventLogRepo;

    @Autowired
    private DataStashUnitTaskEventLogRepository unitTaskEventLogRepo;


    @RequestMapping(value = "unit/state/all", method = RequestMethod.GET)
    public @ResponseBody
    Page<DataStashUnitStateLog> getUnitStates(Model model,
                                                  HttpServletRequest request,
                                                  @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                  @RequestParam(value = "unitCode", defaultValue = "all") String unitCode,
                                                  @RequestParam(value = "batchNo", defaultValue = "all") String batchNo,
                                                  @RequestParam(value = "isTurnover", defaultValue = "all") String isTurnover,
                                                  @RequestParam(value = "state", defaultValue = "all") String state,
                                                  @RequestParam(value = "sortColumn", defaultValue = "lastupd") final String sortColumn,
                                                  @RequestParam(value = "desc", defaultValue = "desc") final String desc) throws  Exception{
        Page<DataStashUnitStateLog> pages =unitStateLogRepo.findUnitStateLog(pageNum,pageSize,unitCode,batchNo,isTurnover,state,sortColumn,desc);
        return pages;
    }


    @RequestMapping(value = "unit/taskstate/all", method = RequestMethod.GET)
    public @ResponseBody
    Page<DataStashUnitTaskStateLog> getUnitTaskStates(Model model,
                                                      HttpServletRequest request,
                                                      @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                      @RequestParam(value = "unitProcId", defaultValue = "all") String unitProcId,
                                                      @RequestParam(value = "unitCode", defaultValue = "all") String unitCode,
                                                      @RequestParam(value = "taskType", defaultValue = "all") String taskType,
                                                      @RequestParam(value = "batchNo", defaultValue = "all") String batchNo,
                                                      @RequestParam(value = "isTurnover", defaultValue = "all") String isTurnover,
                                                      @RequestParam(value = "state", defaultValue = "all") String state,
                                                      @RequestParam(value = "sortColumn", defaultValue = "lastupd")  String sortColumn,
                                                      @RequestParam(value = "desc", defaultValue = "desc")  String desc) throws  Exception{
        Page<DataStashUnitTaskStateLog> pages =unitTaskStateLogRepo.findUnitTaskStateLog( pageNum,  pageSize,  unitProcId, taskType, unitCode, batchNo, state,sortColumn,desc);
        return pages;
    }


    @RequestMapping(value = "unit/state/eventlog", method = RequestMethod.GET)
    public @ResponseBody
    Page<DataStashUnitEventLog> getUnitEventLogs(Model model,
                                              HttpServletRequest request,
                                              @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                             @RequestParam(value = "unitProcId", defaultValue = "all") String unitProcId,
                                             @RequestParam(value = "unitCode", defaultValue = "all") String unitCode,
                                              @RequestParam(value = "batchNo", defaultValue = "all") String batchNo,
                                              @RequestParam(value = "isTurnover", defaultValue = "all") String isTurnover,
                                              @RequestParam(value = "state", defaultValue = "all") String state,
                                              @RequestParam(value = "sortColumn", defaultValue = "createDt") final String sortColumn,
                                              @RequestParam(value = "desc", defaultValue = "desc") final String desc) throws  Exception{
        Page<DataStashUnitEventLog> pages =unitEventLogRepo.findUnitEventLog(pageNum,pageSize,unitProcId,unitCode,batchNo,isTurnover,state,sortColumn,desc);
        return pages;
    }

    @RequestMapping(value = "unit/taskstate/eventlog", method = RequestMethod.GET)
    public @ResponseBody
    Page<DataStashUnitTaskEventLog> getUnitTaskEventLogs(Model model,
                                                         HttpServletRequest request,
                                                         @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                         @RequestParam(value = "taskProcId", defaultValue = "all") String taskProcId,
                                                         @RequestParam(value = "unitProcId", defaultValue = "all") String unitProcId,
                                                         @RequestParam(value = "unitCode", defaultValue = "all") String unitCode,
                                                         @RequestParam(value = "taskType", defaultValue = "all") String taskType,
                                                         @RequestParam(value = "batchNo", defaultValue = "all") String batchNo,
                                                         @RequestParam(value = "isTurnover", defaultValue = "all") String isTurnover,
                                                         @RequestParam(value = "state", defaultValue = "all") String state,
                                                         @RequestParam(value = "sortColumn", defaultValue = "createDt") final String sortColumn,
                                                         @RequestParam(value = "desc", defaultValue = "desc") final String desc) throws  Exception{
        Page<DataStashUnitTaskEventLog> pages =unitTaskEventLogRepo.findUnitTaskEventLog( pageNum,  pageSize,taskProcId,  unitProcId, taskType, unitCode, batchNo, state,sortColumn,desc);
        return pages;
    }
}